package com.dayu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

import com.vecspace.web.filter.ContextFilter;
import com.vecspace.web.filter.ServletRequestMonitorFilter;
import com.vecspace.web.servlet.BaseRestServlet;
import com.vecspace.web.servlet.ResourceServlet;

/**自定义filter和servlet
 */
@Configuration
public class WebConfig {
	
	@Bean
	@ConditionalOnProperty(name="web.filter.first-filter.enabled", havingValue="true", matchIfMissing=true)
    public FilterRegistrationBean<FirstFilter> firstFilter() {
        FilterRegistrationBean<FirstFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setName("firstFilterBean");
        FirstFilter firstFilter = new FirstFilter();
        registrationBean.setFilter(firstFilter);
        registrationBean.setOrder(1);
        List<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add("/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }
	
	//将request，response放到HttpContextThreadLocal中，以便在service中取用
	@Bean
	@ConditionalOnProperty(name="web.filter.context-filter.enabled", havingValue="true", matchIfMissing=true)
    public FilterRegistrationBean<ContextFilter> contextFilter() {
        FilterRegistrationBean<ContextFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setName("contextFilterBean");
        ContextFilter contextFilter = new ContextFilter();
        registrationBean.setFilter(contextFilter);
        registrationBean.setOrder(3);
        List<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add("/api/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }
	
	/**
	 */
	@Bean
	@ConditionalOnProperty(name="web.filter.request-monitor-filter.enabled", havingValue="true", matchIfMissing=true)
    public FilterRegistrationBean<ServletRequestMonitorFilter> servletRequestMonitorFilter() {
        FilterRegistrationBean<ServletRequestMonitorFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setName("requestMonitorFilterBean");
        ServletRequestMonitorFilter filter = new ServletRequestMonitorFilter();        
        registrationBean.setFilter(filter);
        registrationBean.setOrder(30);
        List<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add("/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }
	
	/**检查要访问的url，是否在授权范围内
	 */
// 统一在门户网关进行验证
//	@Bean
//	@ConditionalOnProperty(name="web.filter.auth-filter.enabled", havingValue="true", matchIfMissing=true)
//    public FilterRegistrationBean<AuthFilter> authFilter() {
//        FilterRegistrationBean<AuthFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setName("authFilterBean");
//        AuthFilter authFilter = new AuthFilter();
//        Map<String,String> initParameters = new HashMap<>();
//        registrationBean.setInitParameters(initParameters);
//        registrationBean.setFilter(authFilter);
//        registrationBean.setOrder(9);
//        List<String> urlPatterns = new ArrayList<String>();
//        urlPatterns.add("/irest/*"); 
//        registrationBean.setUrlPatterns(urlPatterns);
//        return registrationBean;
//    }
	
//	@Bean
//	@ConditionalOnProperty(name="web.filter.saas-tenant-filter.enabled", havingValue="true", matchIfMissing=true)
//    public FilterRegistrationBean<SaasTenantFilter> servletSaasTenantFilter() {
//        System.out.println("开始注册SaasTenantFilter");
//		FilterRegistrationBean<SaasTenantFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setName("saasTenantFilterBean");
//        SaasTenantFilter filter = new SaasTenantFilter(); 
//         
//        registrationBean.setFilter(filter);
//        registrationBean.setOrder(6); //SaasTenantFilter要放在最前面，因为租户id可以通过url修改，修改租户id时，会清空session（包括用户登录信息），然后再进入LogonFilter和AuthFilter
//        List<String> urlPatterns = new ArrayList<String>();
//        urlPatterns.add("/irest/*");
//        urlPatterns.add("/rest/*");
//        registrationBean.setUrlPatterns(urlPatterns);        
//        return registrationBean;
//    }
	
//
//	@Bean
//	@ConditionalOnProperty(name="web.filter.spring-session.enabled", havingValue="true", matchIfMissing=true)
//    public FilterRegistrationBean springSessionFilterBean() {
//        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//        registrationBean.setName("springSessionFilterBean");
//        DelegatingFilterProxy filter = new DelegatingFilterProxy();
//        //filter.setBeanName("springSessionFilter");
//        registrationBean.setFilter(filter);
//        registrationBean.setOrder(0);
//        List<String> urlPatterns = new ArrayList<String>();
//        urlPatterns.add("/irest/*");
//        urlPatterns.add("/rest/*");
//        urlPatterns.add("/q/*");
//        urlPatterns.add("*.jsp");
//        registrationBean.setUrlPatterns(urlPatterns);
//        return registrationBean;
//    }

	@Bean  
    public ServletRegistrationBean<DispatcherServlet> dispatcherRegistration(DispatcherServlet dispatcherServlet) {  
		ServletRegistrationBean<DispatcherServlet> dispatcherServletBean = new ServletRegistrationBean<>(dispatcherServlet,"/ws/*");
		dispatcherServletBean.setOrder(5);
		return dispatcherServletBean;
    }
	
	@Bean
	@ConditionalOnProperty(name="web.servlet.base-rest-servlet.enabled", havingValue="true", matchIfMissing=true)
    public ServletRegistrationBean<BaseRestServlet> baseRestServlet() {
		ServletRegistrationBean<BaseRestServlet> servletRegistrationBean = new ServletRegistrationBean<>(new BaseRestServlet(),"/api/*");
		servletRegistrationBean.setOrder(3);
		return servletRegistrationBean;
    }
	
	@Bean
	@ConditionalOnProperty(name="web.servlet.resource-servlet.enabled", havingValue="true", matchIfMissing=true)
    public ServletRegistrationBean<ResourceServlet> resourceServlet() {
		ServletRegistrationBean<ResourceServlet> servletRegistrationBean = new ServletRegistrationBean<>(new ResourceServlet(),"/");
		Map<String,String> initParameters = new HashMap<>();
		initParameters.put("welcomeFile", "index.html");
		servletRegistrationBean.setInitParameters(initParameters);
		servletRegistrationBean.setOrder(10);
		return servletRegistrationBean;
    }
}
